//
//  ComposePhotosView.m
//  Weibo
//
//  Created by Anasue on 9/16/15.
//  Copyright (c) 2015 Anasue. All rights reserved.
//

#import "ComposePhotosView.h"

@implementation ComposePhotosView

- (void)addImage:(UIImage *)image
{
    self.userInteractionEnabled = YES;
    //添加图片
    UIImageView *imageView = [[UIImageView alloc]init];
    imageView.image = image;
    
    [self addSubview:imageView];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    NSInteger count = self.subviews.count;
    int maxColPerRow = 4;
    CGFloat imageViewMargin = 10;
    
    CGFloat imageViewW = (self.width - (maxColPerRow + 1)*imageViewMargin)/maxColPerRow;
    CGFloat imageViewH = imageViewW;
    for (int i = 0; i < count; i ++) {
        UIImageView *imageView = self.subviews[i];
        int col = i % maxColPerRow;
        int row = i / maxColPerRow;
        imageView.width = imageViewW;
        imageView.height = imageViewH;
        imageView.x = col *(imageViewW + imageViewMargin) + imageViewMargin;
        imageView.y = row *(imageViewH + imageViewMargin);
    }
}

- (NSArray *)images
{
    NSMutableArray *array = [NSMutableArray array];
    for (UIImageView *imageView in self.subviews) {
        [array addObject:imageView];
    }
    return array;
}


@end
